var commodityId;
$(function(){
    commodityId = getUrlParam('commodityId');
    if(commodityId==undefined){
        commodityId=1;
    }
    //初始化加载页面
    $.ajax({
        type: "POST",
        url: "http://cn.pn.com/api-pn/comment/api/p/listMain",
        data: {commodityId:commodityId,pageNo:1,pageSize:10},
        dataType: "json",
        success: function (data) {
            var comment=$(".comment").empty();
            $(".commodityName").text(data.commodity.commodityName);//商品名称
            if(data.commodity.star==5){
                $(".commodityStar").text("10.");//星级
            }
            //$(".commodityImg").src=data.commodity.imgUrl;//图片路径
            $(".commodityPlace").text(data.commodity.placeName);//场地名称
            var startTime=getLocalTime(data.commodity.startTime);
            var endTime=getLocalTime(data.commodity.endTime);
            $(".startTime").text(startTime);//开始时间
            $(".endTime").text(endTime);//结束时间
            if(data.commodity.status==1){
                $(".status").text("售票中");
            } else if(data.commodity.status==2){
                $(".status").text("暂停售票");
            } else if(data.commodity.status==3){
                $(".status").text("已过期");
            }
            $(".commodityPrice").text(data.commodity.disPrice);//最低价格

            $.each(data.page.rows,function (i,e) {
                //var imgUrl="../img/11-7.png";
                comment.append("<ul class='comment-cennter'>" +
                    "        <li class='comment-cennter1'>" +
                    "        <div class='comment-cennter1_l'>" +
                    "        <div class='comment-cennter1_l1'><img src='"+e.user.headImgURL+"' alt='' style='width:100%;height: 100%'></div>" +
                    "        <div class='comment-cennter1_l2'>"+e.user.userIdentity+"</div></div>" +
                    "        <div style='border-bottom: 1px solid #999;width:550px;' >" +
                    "        <div style='margin-top:25px;text-indent:1em;font-size:14px;height:100%;overflow:hidden;height:20px;'>" +
                    "        <span style='float:left;font-size:14px;'>"+e.user.nickName+"</span>" +
                    "        <span class='commentTime' style='float: right;'>"+ getLocalTime(e.commentTime)+"</span></div>" +
                    "        <div class='comment-cennter1_c"+e.level+"'></div>" +
                    "        <div class='comment-cennter1_ct'>"+e.content+"</div>" +
                    "       <div style='text-align: left;margin-top: 20px;'>" +
                    "        <img src=''/></div>" +
                    "        <div class='comment-cennter1_r'>" +
                    "        <div class='praise'>" +
                    "        <img src='../img/1-icon-reply.4c1a85db.png' alt='' style='width:20px;height:20px'>" +
                    "        </div>" +
                    "        <a href='http://localhost:8080/static/html/myComments.html?commodityId="+e.commodityId+"&commentId="+e.commentId+"' style='text-decoration: none;margin-left:5px;font-size:13px;color:black;'><p style='width:40px;'><span class='childCount'>"+e.childCount+"</span>回复</p></a>" +
                    "        <div style='width:18px;height:18px;cursor: pointer;margin-left:20px' >" +
                    "        <img src='../img/1-icon-like.479b123c.png' alt='' style='width:18px;height:18px'>" +
                    "        </div>" +
                    "        <a href='javascript:void(0)' style='text-decoration: none;margin-left:5px;font-size:13px;color:black'><span class='likeCount'>"+e.likeCount+"</span>赞</a></div></div></li></ul>");
            });
            //绑定分页信息
            $("#pageNo").text(data.page.curPage);//当前页数
            $("#totalPage").text(data.page.pageCount);//总页数
            if(data.page.curPage<data.page.pageCount){
                $("#load-more").show();
            }else{
                $("#load-more").hide();
            }
        },
        error: function () {
            layer.msg('加载评论失败，请尝试刷新', {
                time: 2000, //2s后自动关闭
            });
        }
    });

    //加载更多
    $("#load-more").click(function () {
        var pageNo=$("#pageNo").text();
        pageNo++;
        //初始化加载页面
        $.ajax({
            type: "POST",
            url: "http://cn.pn.com/api-pn/comment/api/p/listMain",
            data: {commodityId:commodityId,pageNo:pageNo,pageSize:10},
            dataType: "json",
            success: function (data) {
                var comment=$(".comment");
                $.each(data.page.rows,function (i,e) {
                    //var imgUrl="../img/11-7.png";
                    comment.append("<ul class='comment-cennter'>" +
                        "        <li class='comment-cennter1'>" +
                        "        <div class='comment-cennter1_l'>" +
                        "        <div class='comment-cennter1_l1'><img src='"+e.user.headImgURL+"' alt='' style='width:100%;height: 100%'></div>" +
                        "        <div class='comment-cennter1_l2'>"+e.user.userIdentity+"</div></div>" +
                        "        <div style='border-bottom: 1px solid #999;width:550px;' >" +
                        "        <div style='margin-top:25px;text-indent:1em;font-size:14px;height:100%;overflow:hidden;height:20px;'>" +
                        "        <span style='float:left;font-size:14px;'>"+e.user.nickName+"</span>" +
                        "        <span class='commentTime' style='float: right;'>"+ getLocalTime(e.commentTime)+"</span></div>" +
                        "        <div class='comment-cennter1_c"+e.level+"'></div>" +
                        "        <div class='comment-cennter1_ct'>"+e.content+"</div>" +
                        "       <div style='text-align: left;margin-top: 20px;'>" +
                        "        <img src=''/></div>" +
                        "        <div class='comment-cennter1_r'>" +
                        "        <div class='praise'>" +
                        "        <img src='../img/1-icon-reply.4c1a85db.png' alt='' style='width:20px;height:20px'>" +
                        "        </div>" +
                        "        <a href='http://localhost:8080/static/html/myComments.html?commodityId="+e.commodityId+"&commentId="+e.commentId+"' style='text-decoration: none;margin-left:5px;font-size:13px;color:black;'><p style='width:40px;'><span class='childCount'>"+e.childCount+"</span>回复</p></a>" +
                        "        <div style='width:18px;height:18px;cursor: pointer;margin-left:20px' >" +
                        "        <img src='../img/1-icon-like.479b123c.png' onclick='doLike("+e.commodityId+",15)' alt='' style='width:18px;height:18px'>" +
                        "        </div>" +
                        "        <a href='javascript:void(0)' style='text-decoration: none;margin-left:5px;font-size:13px;color:black'><span class='likeCount'>"+e.likeCount+"</span>赞</a></div></div></li></ul>");

                });
                //绑定分页信息
                $("#pageNo").text(data.page.curPage);//当前页数
                $("#totalPage").text(data.page.pageCount);//总页数
                if(data.page.curPage<data.page.pageCount){
                    $("#load-more").show();
                }else{
                    $("#load-more").hide();
                }
            },
            error: function () {
                layer.msg('加载评论失败，请尝试刷新', {
                    time: 2000, //2s后自动关闭
                });
            }
        });
    });

    //评论
    $("#doComment").click(function () {
        location.href="http://localhost:8080/static/html/woyaopinglun.html?commodityId="+commodityId;
        /*
        //获取用户id
        var userData=User.getUserInfo.execution();
        if(userData==undefined){
            layer.msg('请先登录哦~', {
                time: 2000, //2s后自动关闭
            });
        }else{
            var userId=userData.userId;
            //查询该用户是否对该商品有评论，如果有，则跳转wodepinglun（我的评论页面），否则跳我的评论页面
            $.ajax({
                type: "POST",
                url: "http://cn.pn.com/api-pn/comment/api/p/findMain",
                data: {userId:userId,commodityId:commodityId},
                dataType: "json",
                success: function (data) {
                    if(data!=null){
                        location.href="http://localhost:8080/static/html/wodepinglun.html?userId="+userId+"&commodityId="+commodityId+"";
                    }else{
                        location.href="http://localhost:8080/static/html/woyaopinglun.html?userId="+userId+"&commodityId="+commodityId+"";
                    }
                },
                error: function () {
                    location.reload();
                }

            });


        }
        */
    });





    // 全部演出
    $(".nav .lei li.ashow").mouseenter(function(){
        $(".nav .lei li.ashow .submenu").fadeIn()
    });
    $(".nav .lei li.ashow").mouseleave(function(){
        $(".nav .lei li.ashow .submenu").fadeOut()
    });
    // 手机票牛
    $(".nav .right .app").mouseenter(function(){
        $(".nav .right .app .submenu1").fadeIn()
    });
    $(".nav .right .app").mouseleave(function(){
        $(".nav .right .app .submenu1").fadeOut()
    });
    //问号隐藏
    $(".type-desc .line img:eq(1)").mouseenter(function(){
        $(".info-desc").fadeIn()
    });
    $(".type-desc .line img.wen").mouseleave(function(){
        $(".info-desc").fadeOut()
    });

    $(".content .content-left .content1-nav a").click(function(){
        $(".content .content-left .content1-nav a").removeClass("active");
        $(this).addClass("active");
    })
    // 想看
    var num = 0;
    $(".wish p.text1").click(function(){
        if(num==0){
            num=1;
            $(".suc").css({"display":"block"});
            $(this).css({"background": "url(../img/1-heart.aaa0e468.png) no-repeat center 5px","backgroundSize":"19px 19px"})
        }else{
            $(".suc").css({"display":"block"});
            $(".suc").html("取消关注")
            $(this).css({"background": "url(../img/1-heart-thin-grey.ae0c2813.png) no-repeat center 5px","backgroundSize":"19px 19px"})
        }

        var wait=10;
        var timer = null;
        clearInterval(timer)
        timer=setInterval(function(){
            wait--;
            console.log(wait)
            if(wait==0){
                clearInterval(timer)
                $(".suc").fadeOut()
            }
        },1000)

    })
    $(".select .items>p").click(function(){
        $(".select .items>p").removeClass("selected")
        $(this).addClass("selected")

    })

    $("div.item").click(function(){
        $("div.item").removeClass("selected")
        $(this).addClass("selected")
        $(".price .sum1 span").html(parseInt($(this).html()))
        $(".price .common").html(parseInt($(this).html())+"/张")
        var number =  $(".sum1 span").html()
        console.log(number)
        number1 = number
    })
    var number =  $(".sum1 span").html()
    $("li.item").click(function(){
        $(".sum1 span").html(number*$(this).html())
    })
    $("li.item").click(function(){
        $("li.item").removeClass("selected")
        $(this).addClass("selected")
        $(".sum1 span").html(number1*$(this).html())
        // console.log($(this).html())
        // console.log($(".sum1 span").html())
    })
    $(window).on("scroll",function(){
        var scrollT = $(document).scrollTop();
        var height = $(".content .content-left .content1").offset().top;
        if(scrollT >= height){
            $(".content .content-left .btn-show").css({
                "display":"block"
            });
        }else{
            $(".content .content-left .btn-show").css({
                "display":"none"
            });
        }
    });
});

//点赞
function doLike(commentId,userId) {


}

//获取url中的commodityId参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
//转换时间戳为日期格式
function getLocalTime(nS) {
    //将时间戳（十三位时间戳，也就是带毫秒的时间戳）转换成时间格式
    let date = new Date(nS);
    let year = date.getFullYear();
    let month = date.getMonth()+1;
    let day = date.getDate();
    month = month < 10 ? "0"+month:month;
    day = day < 10 ? "0"+day:day;
    date = year+'-'+month+'-'+day;
    return date;
}

